﻿<%@ Page Language="C#"  AutoEventWireup="true" CodeFile="HienGioHang.aspx.cs" Inherits="HienGioHang" Title="Hiện giỏ hàng" %>

<script type="text/css" href="jquery-ui/themes/ui-lightness/ui.all.css" rel="Stylesheet" />
<script type="text/javascript" src="jquery-ui/ui/ui.core.js"></script>
<script type="text/javascript" src="jquery-ui/ui/ui.dialog.js"></script>
<script type="text/javascript">
    $(document).ready(function()
    {
        $("#divDelProduct").dialog({ autoOpen: false });
	    $("#divDelProduct").dialog(
            { 
                
            }
 
        );
		
    });
    function delProduct(index)
    {
        $("#divDelProduct").dialog('option','buttons',
            {
                "Không": function() { 
                        $("#divDelProduct").dialog("close"); 
                    },
                "Có": function() { 
                        var url="/WebSite/Controllers/xlDelHangTrongGio.aspx?btnDel=1&index="+index;
                        jQuery.get(url, function(data)
                        {
                            if(data == "-1")
                                window.location = "GioHang.aspx";
                            else
                                $("#divGioHangCondition").html(data);
                        });
                        $("#divDelProduct").dialog("close");
            } 
        });
        $("#divDelProduct").dialog('option','title','Bạn có muốn xóa sản phẩm?');
        $("#divDelProduct").dialog("open");
    }
    function changeQuantity(index) {
        var newQuantity = document.getElementById("txtSoLuong" + index).value;
        if (isNaN(newQuantity) == true) {
            alert("Bạn phải nhập số");
            return;
        }
        var url = "/WebSite/Controllers/xlThayDoiSoLuong.aspx?changeState=1&index=" + index + "&newQuantity=" + newQuantity;
        jQuery.get(url, function(data) {
            $("#divGioHangCondition").html(data);
        });
    }
</script>
<%
    if (Session["GioHang"] == null)
    {  
%>
    <table width="100%" border="0" align="center" cellpadding="4" cellspacing="0">
      <tr>
        <td class="table_header">Giỏ hàng</td>
      </tr>
      <tr>
        <td align="center"><b>Hiện chưa có sản phẩm nào trong giỏ hàng.</b></td>
      </tr>
      <tr>
        <td align="center"><a href="Default.aspx">Quay về trang chủ?</a></td>
      </tr>
    </table>
<%
    }
    else
    {
%>
<div id="divGioHangCondition">
    <table width="100%" border="0" align="center" cellpadding="4" cellspacing="0">
      <tr>
        <td class="table_header">Giỏ hàng</td>
      </tr>
      <tr>
        <td>
        	<br />
        	<table width="95%" border="1" align="center" cellpadding="4" cellspacing="0" id="table_GioHang">
              <tr>
                <td class="giohang_Header">STT</td>
                <td class="giohang_Header">Tên sản phẩm</td>
                <td class="giohang_Header">Số lượng</td>
                <td class="giohang_Header">Đơn giá (ngàn)</td>
                <td class="giohang_Header">Thành tiền (ngàn)</td>
                <td class="giohang_Header">Xóa</td>
              </tr>
            <% DTO.GioHangDTO GioHang = (DTO.GioHangDTO)Session["GioHang"];
               long ThanhTien = 0;
               long TongThanhTien = 0;
               
               int j;
               for (int i = 0; i < GioHang.lstHang.Count; i++ )
               {
                   j = i + 1;
                   ThanhTien = GioHang.lstHang[i].SoLuong * GioHang.lstHang[i].GiaBanHienTai;
                   TongThanhTien += ThanhTien;
                   DTO.DanhMucSanPhamDTO temp = DAO.DanhMucSanPhamDAO.LayDanhMucSanPhamTheoID(GioHang.lstHang[i].MaDanhMucSanPham);
                   if(temp.MaNhomDanhMucSanPham == 1)
                   {
                       Response.Write("<tr><td>" + j + "</td><td><a href='ChiTietLapTop.aspx?ID=" + GioHang.lstHang[i].ID + "'><img class='hangTrongGio' src='" + GioHang.lstHang[i].HinhSanPham + "' alt='' border='0'/></a><div style='padding-top:20px'><a class='linkStyle' href='ChiTietLapTop.aspx?ID=" + GioHang.lstHang[i].ID + "'>" + GioHang.lstHang[i].TenSanPham + "</a></div></td><td><input type='text' size='2' ID='txtSoLuong" + i + "' value='" + GioHang.lstHang[i].SoLuong + "' Width='30px' onKeyUp='changeQuantity(" + i + ");'/></td><td>" + GioHang.lstHang[i].GiaBanHienTai + "</td><td>" + ThanhTien + "</td><td><img src='images/cancel_48.png' width='20' height='20' alt='' onclick='delProduct(" + i + ");' /></td></tr>");
                   }
                   else if (temp.MaNhomDanhMucSanPham == 2)
                   {
                       Response.Write("<tr><td>" + j + "</td><td><a href='ChiTietDienThoai.aspx?ID=" + GioHang.lstHang[i].ID + "'><img class='hangTrongGio' src='" + GioHang.lstHang[i].HinhSanPham + "' alt='' border='0'/></a><div style='padding-top:20px'><a class='linkStyle' href='ChiTietDienThoai.aspx?ID=" + GioHang.lstHang[i].ID + "'>" + GioHang.lstHang[i].TenSanPham + "</a></div></td><td><input type='text' size='2' ID='txtSoLuong" + i + "' value='" + GioHang.lstHang[i].SoLuong + "' Width='30px' onKeyUp='changeQuantity(" + i + ");'/></td><td>" + GioHang.lstHang[i].GiaBanHienTai + "</td><td>" + ThanhTien + "</td><td><img src='images/cancel_48.png' width='20' height='20' alt='' onclick='delProduct(" + i + ");' /></td></tr>");
                   }
               }
               GioHang.ThanhTien = TongThanhTien;
               Response.Write("<tr><td colspan='4' class='totalCostText'>Tổng thành tiền:</td><td class='totalCost' colspan='3'>" + GioHang.ThanhTien + "</td><td></td></tr>");
               Session["GioHang"] = GioHang;
               %>
            </table>        
        </td>
      </tr>
    </table>
</div>
<div id='divDelProduct'></div>
<form id="frmCart" runat="server">
    <table width="100%">
        <tr>
            <td>
                <hr /><br /><br />
                <div style="text-align:center">
                    <asp:Button ID="btnSubmit" runat="server" onclick="btnSubmit_Click" 
                        Text="Xác nhận thông tin" />
                    &nbsp;
                    <asp:Button ID="btnDelGioHang" runat="server" onclick="btnDelGioHang_Click" 
                        Text="Huỷ Giỏ Hàng" />
                    &nbsp;
                    <asp:Button ID="btnContDatHang" runat="server" onclick="btnContDatHang_Click" 
                        Text="Tiếp tục chọn hàng" />
                </div>
                <br /><br />
            </td>
        </tr>
    </table> 
</form>

<%
    }
%>
